package com.twinkle.service.impl;

import com.twinkle.dao.ContactsDao;
import com.twinkle.bean.Contacts;
import com.twinkle.dao.UCDao;
import com.twinkle.service.ContactsService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContactsServiceImpl implements ContactsService {

    private final ContactsDao contactsDao;

    private final UCDao ucDao;

    public ContactsServiceImpl(ContactsDao contactsDao, UCDao ucDao) {
        this.contactsDao = contactsDao;
        this.ucDao = ucDao;
    }

    @Override
    public List<Contacts> selectSContactLike(Integer user_id, String para_name) {
        return contactsDao.selectContactsLike(user_id, para_name);
    }

    @Override
    public List<Contacts> selectSContact(Integer user_id) {
        return contactsDao.selectContactsAll(user_id);
    }

    @Override
    public boolean insertSContacts(Contacts para_contacts) {
        int check_1 = contactsDao.insertContacts(para_contacts);
        if (check_1 > 0) {
            int check_2 = ucDao.insertUC(para_contacts.getContacts_id(), contactsDao.selectContactsOne());
            return check_2 > 0;
        }
        return false;
    }

    @Override
    public boolean deleteSContacts(Integer para_contactsId) {
        Integer temp = ucDao.selectUC(para_contactsId);
        int check_1 = ucDao.deleteUC(temp);
        if (check_1 > 0) {
            int check_2 = contactsDao.deleteContacts(para_contactsId);
            return check_2 > 0;
        }
        return false;
    }

    @Override
    public boolean updateSContacts(Contacts para_contacts) {
        int check = contactsDao.updateContacts(para_contacts);
        return check > 0;
    }
}
